/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO.Util;

import Pojo.CauHoi;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

/**
 *
 * @author ducquyen
 */
public class CauHoiDAO {

    public static String layDuongDanTapTinCauHoi()
    {
        return "CauHoi.xml";
    }
    public static ArrayList<CauHoi> layTatCaCauHoi() {
        ArrayList<CauHoi> arrCauHoi = new ArrayList<CauHoi>();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(layDuongDanTapTinCauHoi());

            Element root = doc.getDocumentElement();
            NodeList list = root.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                if (node instanceof Element) {
                    Element element = (Element) node;
                    CauHoi ch = new CauHoi();
                    ch.setMaCauHoi(Integer.parseInt(element.getAttribute("MaCauHoi")));
                    NodeList elist = element.getChildNodes();
                    for (int j = 0; j < elist.getLength(); j++) {
                        Node enode = elist.item(j);
                        if (enode instanceof Element) {
                            Element ele = (Element) enode;
                            Text textNode = (Text) ele.getFirstChild();
                            String content = textNode.getData();
                            switch (ele.getTagName()) {
                                case "NoiDung":
                                    ch.setNoiDungCauHoi(content);
                                    break;
                                case "LuaChon1":
                                    ch.setLuaChon1(content);
                                    break;
                                case "LuaChon2":
                                    ch.setLuaChon2(content);
                                    break;
                                case "LuaChon3":
                                    ch.setLuaChon3(content);
                                    break;
                                case "LuaChon4":
                                    ch.setLuaChon4(content);
                                    break;
                                case "DapAn":
                                    ch.setCauDung(Integer.parseInt(content));
                                    break;
                                case "DoKho":
                                    ch.setDoKho(Integer.parseInt(content));
                                    break;
                            }

                        }

                    }
                    //Add cau hoi vao list 
                    arrCauHoi.add(ch);
                }

            }

            for (int k = 0; k < arrCauHoi.size(); k++) {
                CauHoi cho = arrCauHoi.get(k);
                System.out.println(cho.getNoiDungCauHoi());
            }
        } catch (Exception e) {
        }
        return arrCauHoi;
    }
  
    public static ArrayList<CauHoi> timKiemCauHoi(String noiDung) {
        ArrayList<CauHoi> arrKQ = new ArrayList<>();
        ArrayList<CauHoi> arrCauHoi = new ArrayList<>();
        try {
            arrCauHoi = layTatCaCauHoi();
            for (int i = 0; i < arrCauHoi.size(); i++) {
                CauHoi ch = arrCauHoi.get(i);

                if (ch.getNoiDungCauHoi().contains(noiDung)==true) {
                    arrKQ.add(ch);
                }
            }
        } catch (Exception e) {
        }

        return arrKQ;
    }

    public static void capNhatCauHoi(Document doc, CauHoi ch) throws TransformerConfigurationException, TransformerException {

        Element root = doc.getDocumentElement();
        NodeList list = root.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;

                if (Integer.parseInt(element.getAttribute("MaCauHoi")) == ch.getMaCauHoi()) {
                    NodeList elist = element.getChildNodes();
                    for (int j = 0; j < elist.getLength(); j++) {
                        Node enode = elist.item(j);
                        if (enode instanceof Element) {
                            Element ele = (Element) enode;
                            Text textNode = (Text) ele.getFirstChild();
                            String content = textNode.getData();
                            switch (ele.getTagName()) {
                                case "NoiDung":
                                    ele.setTextContent(ch.getNoiDungCauHoi());
                                    break;
                                case "LuaChon1":
                                    ele.setTextContent(ch.getLuaChon1());
                                    break;
                                case "LuaChon2":
                                    ele.setTextContent(ch.getLuaChon2());
                                    break;
                                case "LuaChon3":
                                    ele.setTextContent(ch.getLuaChon3());
                                    break;
                                case "LuaChon4":
                                    ele.setTextContent(ch.getLuaChon4());
                                    break;
                                case "DapAn":
                                    ele.setTextContent(Integer.toString(ch.getCauDung()));
                                    break;
                                case "DoKho":
                                    ele.setTextContent(Integer.toString(ch.getDoKho()));
                                    break;
                            }
                        }
                    }

                }
            }

        }
        //Ghi file
        ghiFile(doc);

    }

    private static void ghiFile(Document doc) throws TransformerConfigurationException, TransformerException {

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(layDuongDanTapTinCauHoi());

        transformer.transform(source, result);

    }

    public static CauHoi layCauHoiTheoMaCauHoi(Document doc, int maCauHoi) {
        CauHoi kq = new CauHoi();
        Element root = doc.getDocumentElement();
        NodeList list = root.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element element = (Element) node;
                int ma = Integer.parseInt(element.getAttribute("MaCauHoi"));
                if (ma == maCauHoi) {
                    {
                        kq.setMaCauHoi(ma);
                        NodeList elist = element.getChildNodes();
                        for (int j = 0; j < elist.getLength(); j++) {
                            Node enode = elist.item(j);
                            if (enode instanceof Element) {
                                Element ele = (Element) enode;
                                Text textNode = (Text) ele.getFirstChild();
                                String content = textNode.getData();
                                switch (ele.getTagName()) {
                                    case "NoiDung":
                                        kq.setNoiDungCauHoi(content);
                                        break;
                                    case "LuaChon1":
                                        kq.setLuaChon1(content);
                                        break;
                                    case "LuaChon2":
                                        kq.setLuaChon2(content);
                                        break;
                                    case "LuaChon3":
                                        kq.setLuaChon3(content);
                                        break;
                                    case "LuaChon4":
                                        kq.setLuaChon4(content);
                                        break;
                                    case "DapAn":
                                        kq.setCauDung(Integer.parseInt(content));
                                        break;
                                    case "DoKho":
                                        kq.setDoKho(Integer.parseInt(content));
                                        break;
                                }
                            }
                        }
                    }
                }

            }
        }
        return kq;

    }

    public static void themCauHoi(Document doc, CauHoi ch) throws TransformerConfigurationException, TransformerException {
        Element root = doc.getDocumentElement();
        Element cauHoi = doc.createElement("CauHoi");
        cauHoi.setAttribute("MaCauHoi", Integer.toString(ch.getMaCauHoi()));

        Element noiDung = doc.createElement("NoiDung");
        noiDung.appendChild(doc.createTextNode(ch.getNoiDungCauHoi()));
        cauHoi.appendChild(noiDung);

        Element luaChon1 = doc.createElement("LuaChon1");
        luaChon1.appendChild(doc.createTextNode(ch.getLuaChon1()));
        cauHoi.appendChild(luaChon1);

        Element luaChon2 = doc.createElement("LuaChon2");
        luaChon2.appendChild(doc.createTextNode(ch.getLuaChon2()));
        cauHoi.appendChild(luaChon2);

        Element luaChon3 = doc.createElement("LuaChon3");
        luaChon3.appendChild(doc.createTextNode(ch.getLuaChon3()));
        cauHoi.appendChild(luaChon3);

        Element luaChon4 = doc.createElement("LuaChon4");
        luaChon4.appendChild(doc.createTextNode(ch.getLuaChon4()));
        cauHoi.appendChild(luaChon4);

        Element DapAn = doc.createElement("DapAn");
        DapAn.appendChild(doc.createTextNode(Integer.toString(ch.getCauDung())));
        cauHoi.appendChild(DapAn);

        Element DoKho = doc.createElement("DoKho");
        DoKho.appendChild(doc.createTextNode(Integer.toString(ch.getDoKho())));
        cauHoi.appendChild(DoKho);

        root.appendChild(cauHoi);

        // write the content into xml file


        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(layDuongDanTapTinCauHoi());

        transformer.transform(source, result);

//        System.out.println("File saved!");

    }

    public static int layMaCauHoiTuFile(Document doc) {
        int maCauHoi = -1;
        try {
            Element root = doc.getDocumentElement();
            NodeList list = root.getChildNodes();
            maCauHoi = list.getLength();

        } catch (Exception ex) {
        }
        return maCauHoi;

    }
}
